package library.database;

import com.gooddies.reflection.fieldprocessing.fieldextractors.jdbcExctractor.JdbcFieldFiller;
import java.lang.String;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import library.model.BooksAuthorPojo;
import library.model.Author;
import library.model.Book;
import library.model.Genre;

/**
 * @author Dmitry
 */
public class Data extends AbstractDatabase {
    private static JdbcFieldFiller<Author> authorFieldProcessor = null;
    private static JdbcFieldFiller<Book> booksFieldProcessor = null;
    private static JdbcFieldFiller<Genre> genresFieldProcessor = null;
    private static JdbcFieldFiller<BooksAuthorPojo> booksAuthorFieldProcessor = null;

    static {
        authorFieldProcessor = new JdbcFieldFiller(Author.class);
        booksFieldProcessor = new JdbcFieldFiller(Book.class);
        genresFieldProcessor = new JdbcFieldFiller(Genre.class);
        booksAuthorFieldProcessor = new JdbcFieldFiller(BooksAuthorPojo.class);
    }

    public static Author[] getAuthors() {
        return (Author[]) SQLReturnObjects("select id, first_name, middle_name, last_name from authors where search_name not null",
                null, new FillerReflectionDatabaseCallback(authorFieldProcessor, Author.class));
    }

    public static Author[] getAuthorsByGenre(String genreId) {
        Map<Integer, Object> params = new HashMap<>();
        params.put(1, genreId);
        return (Author[]) SQLReturnObjects("SELECT DISTINCT a.id, a.first_name, a.middle_name, a.last_name FROM authors a, genres g, books b WHERE (a.search_name not null) AND g.id_genre=? AND g.id_book=b.id AND a.id=b.id_author",
                params, new FillerReflectionDatabaseCallback(authorFieldProcessor, Author.class));
    }

    public static BooksAuthorPojo[] getBooksAuthorsByGenre(String genreId) {
        Map<Integer, Object> params = new HashMap<>();
        params.put(1, genreId);
        return (BooksAuthorPojo[]) SQLReturnObjects("SELECT DISTINCT coalesce(b.description, '0') \"description\", b.id \"id\", b.title \"book\", a.full_name \"author\" FROM authors a, genres g, books b WHERE (a.search_name not null) AND g.id_genre=? AND g.id_book=b.id AND a.id=b.id_author order by b.title",
                params, new FillerReflectionDatabaseCallback(booksAuthorFieldProcessor, BooksAuthorPojo.class));
    }

    public static Author getAuthor(int id) {
        Map<Integer, Object> params = new HashMap<>();
        params.put(1, id);
        return (Author) SQLReturnObject("select id, first_name, middle_name, last_name from authors where (search_name not null) and id=?",
                params, new FillerReflectionDatabaseCallback(authorFieldProcessor, Author.class));
    }

    public static Book[] getBooksByAuthor(int authorId) {
        Map<Integer, Object> params = new HashMap<>();
        params.put(1, authorId);
        return (Book[]) SQLReturnObjects("SELECT coalesce(description, '0') \"description\",id, id_author, title, genres, file_name FROM books WHERE id_author=? ",
                params, new FillerReflectionDatabaseCallback(booksFieldProcessor, Book.class));
    }

    public static Genre[] getGenres() {
        return (Genre[]) SQLReturnObjects("SELECT id,  FROM genres ",
                null, new FillerReflectionDatabaseCallback(booksFieldProcessor, Genre.class));
    }
}